package bma.common.langutil.core;

import java.util.Map;

/**
 * 
 * @author guanzhong
 * 
 */
public class StringKeyMap extends MapWrap<String, Object> {

	private static final long serialVersionUID = 1L;

	public StringKeyMap() {
		super();
	}

	public StringKeyMap(int initialCapacity, float loadFactor) {
		super(initialCapacity, loadFactor);
	}

	public StringKeyMap(int initialCapacity) {
		super(initialCapacity);
	}

	public StringKeyMap(Map<String, Object> map) {
		super(map);
	}

	/**
	 * Get a copy of the Map's String keys in an array of Strings.
	 */
	public String[] getKeys() {
		return keySet().toArray(new String[size()]);
	}

	/**
	 * <p>
	 * Adds the given <code>int</code> value to the <code>StringKeyMap</code>.
	 * </p>
	 */
	public void put(String key, int value) {
		super.put(key, Integer.valueOf(value));
	}

	/**
	 * <p>
	 * Adds the given <code>long</code> value to the <code>StringKeyMap</code>.
	 * </p>
	 */
	public void put(String key, long value) {
		super.put(key, Long.valueOf(value));
	}

	/**
	 * <p>
	 * Adds the given <code>float</code> value to the <code>StringKeyMap</code>.
	 * </p>
	 */
	public void put(String key, float value) {
		super.put(key, Float.valueOf(value));
	}

	/**
	 * <p>
	 * Adds the given <code>double</code> value to the <code>StringKeyMap</code>
	 * .
	 * </p>
	 */
	public void put(String key, double value) {
		super.put(key, Double.valueOf(value));
	}

	/**
	 * <p>
	 * Adds the given <code>boolean</code> value to the
	 * <code>StringKeyMap</code>.
	 * </p>
	 */
	public void put(String key, boolean value) {
		super.put(key, Boolean.valueOf(value));
	}

	/**
	 * <p>
	 * Adds the given <code>char</code> value to the <code>StringKeyMap</code>.
	 * </p>
	 */
	public void put(String key, char value) {
		super.put(key, Character.valueOf(value));
	}

	/**
	 * <p>
	 * Adds the given <code>String</code> value to the <code>StringKeyMap</code>
	 * .
	 * </p>
	 */
	public void put(String key, String value) {
		super.put(key, value);
	}

	/**
	 * <p>
	 * Adds the given <code>Object</code> value to the <code>StringKeyMap</code>
	 * .
	 * </p>
	 */
	@Override
	public Object put(String key, Object value) {
		return super.put((String) key, value);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>int</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not an Integer.
	 */
	public int getInt(String key, int def) {
		Object obj = get(key);
		if (obj == null)
			return def;

		if (obj instanceof Number)
			return ((Number) obj).intValue();
		return ValueUtil.intValue(obj.toString(), def);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>long</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not a Long.
	 */
	public long getLong(String key, long def) {
		Object obj = get(key);
		if (obj == null)
			return def;

		if (obj instanceof Number)
			return ((Number) obj).longValue();
		return ValueUtil.longValue(obj.toString(), def);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>float</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not a Float.
	 */
	public float getFloat(String key, float def) {
		Object obj = get(key);
		if (obj == null)
			return def;

		if (obj instanceof Number)
			return ((Number) obj).floatValue();
		return ValueUtil.floatValue(obj.toString(), def);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>double</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not a Double.
	 */
	public double getDouble(String key, double def) {
		Object obj = get(key);
		if (obj == null)
			return def;

		if (obj instanceof Double)
			return ((Number) obj).doubleValue();
		return ValueUtil.doubleValue(obj.toString(), def);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>boolean</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not a Boolean.
	 */
	public boolean getBoolean(String key, boolean def) {
		Object obj = get(key);
		if (obj == null)
			return def;
		if (obj instanceof Boolean)
			return ((Boolean) obj).booleanValue();
		return ValueUtil.booleanValue(obj.toString(), def);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>char</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not a Character.
	 */
	public char getChar(String key, char def) {
		Object obj = get(key);
		if (obj == null)
			return def;

		if (obj instanceof Character)
			return ((Character) obj).charValue();
		return ValueUtil.charValue(obj.toString(), def);
	}

	/**
	 * <p>
	 * Retrieve the identified <code>String</code> value from the
	 * <code>StringKeyMap</code>.
	 * </p>
	 * 
	 * @throws ClassCastException
	 *             if the identified object is not a String.
	 */
	public String getString(String key, String def) {
		Object obj = get(key);
		if (obj == null)
			return def;
		return obj.toString();
	}
}
